﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using System.Diagnostics.Eventing.Reader;

namespace 共享单车管理系统.DDA
{
    public partial class inster_1 : Form
    {
        public inster_1()
        {
            InitializeComponent();
        }
        SqlDataAdapter daDepartment;
        DataSet ds = new DataSet();

        void init()
        {
            DB.GetCn();
            string str = "select * from bm";
            daDepartment = new SqlDataAdapter(str, DB.cn);

            daDepartment.Fill(ds, "department_info");
            DB.cn.Close();
        }

        void showAll()
        {
            DataView dvDepartment = new DataView(ds.Tables["department_info"]);
            dgvDepartment.DataSource = dvDepartment;
            dgvDepartment.Columns[3].Width = 500;
        }

        void dgvHead()
        {
            this.dgvDepartment.Columns[0].HeaderText = "部门编号";
            this.dgvDepartment.Columns[1].HeaderText = "部门名称";
            this.dgvDepartment.Columns[2].HeaderText = "部门经理";
            this.dgvDepartment.Columns[3].HeaderText = "部门描述";
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textid.Text == "" || textname.Text == "")
            {
                MessageBox.Show("部门编号和部门名称不能为空");
            }
            else
            {
                DB.GetCn();
                DialogResult dr = MessageBox.Show("确认添加？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    string sid = "select * from bm where DepartmentID='" + textid.Text + "'";
                    DataTable dt = DB.GetDataSet(sid);
                    if (dt.Rows.Count > 0)
                    {
                        MessageBox.Show("部门已存在");
                    }
                    else
                    {
                        string sn = "select * from bm where bmname='" + textname.Text + "'";
                        DataTable dt2 = DB.GetDataSet(sn);
                        if (dt2.Rows.Count > 0)
                        {
                            MessageBox.Show("部门名称已存在");
                        }
                        else
                        {
                            DataRow Deprow = ds.Tables["department_info"].NewRow();
                            Deprow["DepartmentID"] = textid.Text.Trim(); // 添加部门ID
                            Deprow["bmname"] = textname.Text.Trim();
                            Deprow["bujl"] = textjingli.Text.Trim();
                            Deprow["bmms"] = textmiaoshu.Text.Trim();
                            ds.Tables["department_info"].Rows.Add(Deprow);
                        }
                    }
                }
                else
                {
                    return;
                }

                SqlCommand cmd = new SqlCommand("add_log", DB.cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("username", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("Time", SqlDbType.DateTime));
                cmd.Parameters.Add(new SqlParameter("table", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("Type", SqlDbType.NVarChar)); // 添加 Type 参数
                cmd.Parameters["username"].Value = Login.UserID;
                cmd.Parameters["Time"].Value = DateTime.Now;
                cmd.Parameters["table"].Value = "bm表";
                cmd.Parameters["Type"].Value = "添加"; // 这里设置 Type 参数的值

                try
                {
                    SqlCommandBuilder dbDepartment = new SqlCommandBuilder(daDepartment);
                    daDepartment.Update(ds, "department_info");
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void inster_1_Load(object sender, EventArgs e)
        {
            init();
            showAll();
            dgvHead();
        }
    }
}